{
    "cells": [
        {
            "cell_type": "code",
            "execution_count": 11,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Dev init\n",
                        "0\n",
                        "Chip Num = 0\n"
                    ]
                }
            ],
            "source": [
                "import base_api\n",
                "import numpy\n",
                "import pickle\n",
                "from sdk_array_newsystem import SDKArray\n",
                "obj = base_api.BaseAPI()\n",
                "array = SDKArray(0)\n",
                "\n",
                "obj.devInit()\n",
                "print('Dev init')\n",
                "chipNum = 0\n",
                "obj.selectChip(chipNum)\n",
                "print('Chip Num = %d' % chipNum)\n",
                "def pickle_load(file, **kwargs):\n",
                "    with open(file, 'rb') as f:\n",
                "        return pickle.load(f, **kwargs)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 12,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "(484, 92)\n",
                        "(92, 10)\n",
                        "(484, 92)\n",
                        "(92, 10)\n"
                    ]
                }
            ],
            "source": [
                "# 1.load true weight\n",
                "\n",
                "\n",
                "weight_ = pickle_load('../mnist_demo/chip_weight_03_16.pkl')\n",
                "#print(weight_)\n",
                "weight_2 = pickle_load('../mnist_demo/weight_chip_12_22.pkl')\n",
                "#print(weight_['10'][0][0][:])\n",
                "print(weight_['10'][0].shape)\n",
                "print(weight_['11'][0].shape)\n",
                "print(weight_2['10'][0].shape)\n",
                "print(weight_2['11'][0].shape)\n",
                "w21= weight_2['10'][0]\n",
                "w22= weight_2['11'][0]\n",
                "#input()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 13,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "[[10 15 10  3  4  6  7  8  2 10]\n",
                        " [ 4  3  9 10 15  6  2  9  7 14]\n",
                        " [ 9 11 10  7  7  4 11  8 12  4]\n",
                        " [ 5 13  2  9 10 11  7  9  1 10]\n",
                        " [ 2  5  6  8  8 10  1  6 12  7]\n",
                        " [ 7 12 15 14  5  2  5  8  5 11]\n",
                        " [ 4  2 14  3  4  6  6  7  2  6]\n",
                        " [ 3  9 15  4  4 10 10  2  6  8]\n",
                        " [ 3  7  9  6  9 11  3  9  8  8]\n",
                        " [ 2  6  6  7 11 12 13  5  6  9]]\n"
                    ]
                }
            ],
            "source": [
                "# 2.show part of true weight\n",
                "\n",
                "# x = numpy.zeros((n,m))\n",
                "# for i in range(n):\n",
                "#     for j in range(m):\n",
                "#         x[i][j] = 2\n",
                "x = w22[0:10,:]\n",
                "print(x)\n",
                "# for i in range(1):\n",
                "#     array.set_weight(x,(0,0,10,10))"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 14,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "(92, 10)\n"
                    ]
                },
                {
                    "ename": "ValueError",
                    "evalue": "权重维度 (10, 10) 应为 (92, 10)",
                    "output_type": "error",
                    "traceback": [
                        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
                        "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
                        "\u001b[0;32m<ipython-input-14-11e6517a1f85>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;31m#      array.set_weight(x,(484,92,10,10))\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m     \u001b[0marray\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_weight\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m484\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m92\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m  \u001b[0;31m# start row, row offset, start col, col offset\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      9\u001b[0m \u001b[0;31m# for i in range(5):\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;31m#     array.set_weight(w21,(0,0,484,92))  # layer1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
                        "\u001b[0;32m/home/xilinx/npu_array/npu/sdk_array_newsystem.py\u001b[0m in \u001b[0;36mset_weight\u001b[0;34m(self, weight, addr, form, quant)\u001b[0m\n\u001b[1;32m    274\u001b[0m         \u001b[0mshape\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0my2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    275\u001b[0m         \u001b[0mH\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mW\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 276\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcheck_weight\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mweight\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    277\u001b[0m         \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mweight\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    278\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mselect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
                        "\u001b[0;32m/home/xilinx/npu_array/npu/sdk_array_newsystem.py\u001b[0m in \u001b[0;36mcheck_weight\u001b[0;34m(self, weight, shape)\u001b[0m\n\u001b[1;32m    221\u001b[0m            \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'权重矩阵的数据类型应为整数类型, 而不是 {weight.dtype}'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    222\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mweight\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 223\u001b[0;31m            \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'权重维度 {weight.shape} 应为 {shape}'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    224\u001b[0m         \u001b[0;31m#N = 2**self.PROFILE.weight_bits - 1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    225\u001b[0m         \u001b[0mN\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m15\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
                        "\u001b[0;31mValueError\u001b[0m: 权重维度 (10, 10) 应为 (92, 10)"
                    ]
                }
            ],
            "source": [
                "# 3.weight weight\n",
                "# for i in range(5):\n",
                "#     array.set_weight(x,(0,0,n,m))\n",
                "# x = w22[0:10,0:10]\n",
                "# for i in range(5):\n",
                "#      array.set_weight(x,(484,92,10,10))\n",
                "for i in range(5):\n",
                "    array.set_weight(w22,(484,10,92,10))  # start row, row offset, start col, col offset\n",
                "# for i in range(5):   \n",
                "#     array.set_weight(w21,(0,0,484,92))  # layer1\n",
                "\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 3,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "(1, 484)\n",
                        "calc on chip time:0.0020627975463867188\n"
                    ]
                }
            ],
            "source": [
                "# read weight\n",
                "row=484+3\n",
                "col=88\n",
                "n=1\n",
                "m=16\n",
                "obj.cfgReadPulse(ti=3,ts=10,tb=10,te=10)\n",
                "t = obj.calc_array((0,484,0,92),numpy.random.randint(0,2,(1,484),dtype=numpy.int8))\n",
                "# print(t)\n",
                "\n",
                "# for i in range(row,row+n):\n",
                "#     for j in range(col,col+m):\n",
                "#         ret=obj.calcOneCell(i, j)\n",
                "#         # ret=obj.readOneCell(i,j,'NEG')\n",
                "#         print(ret,end=\" \")\n",
                "#     print()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "import os\n",
                "import sys\n",
                "\n",
                "FILE_PATH = os.path.realpath('')\n",
                "LOG_PATH = FILE_PATH + '/log'\n",
                "\n",
                "savedStdout = sys.stdout\n",
                "logFileName = 'read_result_' + str(chipNum) + '.csv'\n",
                "logFileName = LOG_PATH + '/' + logFileName\n",
                "\n",
                "mapTarget = 9\n",
                "rowCount = 10\n",
                "colCount = 10\n",
                "\n",
                "totalCell = colCount * rowCount\n",
                "cnt = 0\n",
                "\n",
                "with open(logFileName, 'w') as file:\n",
                "    try:\n",
                "        sys.stdout = file\n",
                "        for i in range(rowCount):\n",
                "            for j in range(colCount):\n",
                "                # ret = obj.readOneCell_1(i, j)\n",
                "                ret = obj.calcOneCell(i, j)\n",
                "                if (mapTarget - 1) <= ret <= (mapTarget + 1):\n",
                "                    cnt += 1\n",
                "                print('%02d, ' % ret, end='')\n",
                "            print()\n",
                "    finally:\n",
                "        sys.stdout = savedStdout\n",
                "print('Read All, Accuracy = %f' % (cnt / totalCell))"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 8,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "0 3 2 -3 -2 -2 0 0 -3 1 \n",
                        "-2 -2 0 1 3 -1 -2 1 -1 3 \n",
                        "0 2 1 0 -1 -2 1 0 2 -2 \n",
                        "-1 2 -2 1 1 2 -1 1 -4 1 \n"
                    ]
                }
            ],
            "source": [
                "for i in range(484,488):  # start row, end row\n",
                "    for j in range(92,102):  # start col, end col\n",
                "        ret=obj.calcOneCell(i, j) - 8\n",
                "        print(ret,end=\" \")\n",
                "    print()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "rowCount = 10\n",
                "colCount = 10\n",
                "\n",
                "# form三次，避免读误差影响\n",
                "for _ in range(3):\n",
                "    for i in range(rowCount):\n",
                "        for j in range(colCount):\n",
                "            ret = obj.readOneCell_1(i, j)\n",
                "            if ret == 8:\n",
                "                obj.formOneCell_1(i, j, 4.55, 1.87, 1000000)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "import operation\n",
                "import base_api\n",
                "opObj = operation.Operation()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "addr = operation.AddrInfo()\n",
                "mapTarget = 12\n",
                "addr.colStart = 0\n",
                "addr.colCount = 10\n",
                "addr.rowStart = 0\n",
                "addr.rowCount = 10\n",
                "totalCell = addr.colCount * addr.rowCount\n",
                "mapPara = operation.MapPara()\n",
                "mapPara.setVWLStart = 1.8\n",
                "mapPara.setVWLStep = 0.2\n",
                "mapPara.setVWLEnd = 3.2\n",
                "mapPara.setVBLStart = 2.6\n",
                "mapPara.setVBLStep = 0\n",
                "mapPara.setVBLEnd = 2.6\n",
                "mapPara.rstVWLStart = 3.0\n",
                "mapPara.rstVWLStep = 0.2\n",
                "mapPara.rstVWLEnd = 4.4\n",
                "mapPara.rstVSLStart = 3.8\n",
                "mapPara.rstVSLStep = 0\n",
                "mapPara.rstVSLEnd = 3.8\n",
                "mapPara.errorHigh = 1\n",
                "mapPara.errorLow = 1\n",
                "mapPara.maxProgramNum = 100\n",
                "mapPara.maxCheckNum = 7\n",
                "mapPara.checkThreshold = 6\n",
                "\n",
                "array = [[mapTarget for _ in range(base_api.TOTAL_COL)] for _ in range(base_api.TOTAL_ROW)]\n",
                "# for i in range(20):\n",
                "#     for j in range(20):\n",
                "#         array[i][j] = i % 8 + 8\n",
                "\n",
                "opObj.map(addr, mapPara, array)\n",
                "\n",
                "print('Map All, Accuracy = %f' % (opObj.cellPassCnt / totalCell))"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "import numpy as np\n",
                "rowInput = np.random.randint(0, 2, (3, 10), dtype='uint8')\n",
                "ret = obj.calcArray(rowInput, 0, 0, 10)\n",
                "print(ret)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "rowCount = 10\n",
                "colCount = 10\n",
                "\n",
                "for i in range(rowCount):\n",
                "    for j in range(colCount):\n",
                "        ret = obj.mapSingleDevice_2T2R(i, j, 9)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 7,
            "metadata": {},
            "outputs": [],
            "source": [
                "input_test = pickle_load('../mnist_demo/input_test_binary.pkl')\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 5,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "5 2 8 9 7 7 10 5 9 8 \n",
                        "15 4 10 9 0 11 9 2 9 4 \n",
                        "9 6 8 5 10 4 8 6 11 2 \n",
                        "3 8 8 8 13 13 3 5 10 13 \n",
                        "5 10 8 12 14 5 8 8 6 6 \n"
                    ]
                }
            ],
            "source": [
                "# read weight\n",
                "row=484+40\n",
                "col=92\n",
                "n=5\n",
                "m=10\n",
                "for i in range(row,row+n):\n",
                "    for j in range(col,col+m):\n",
                "        ret=obj.calcOneCell(i, j)\n",
                "        # ret=obj.readOneCell(i,j,'NEG')\n",
                "        print(ret,end=\" \")\n",
                "    print()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 12,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "[[ 2.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.\n",
                        "   0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.\n",
                        "   0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.\n",
                        "   0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.\n",
                        "   0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.\n",
                        "   0.  0.]]\n"
                    ]
                }
            ],
            "source": [
                "out_temp = pickle_load('../mnist_demo/test_output_0316.pkl')\n",
                "print(out_temp['matmul_1'])\n",
                "#print(input_test['input'][4])"
            ]
        }
    ],
    "metadata": {
        "interpreter": {
            "hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
        },
        "kernelspec": {
            "display_name": "Python 3.6.5 64-bit",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.6.5"
        },
        "orig_nbformat": 4
    },
    "nbformat": 4,
    "nbformat_minor": 2
}
